#include "ObCastle.h"
#include "D3DApp.h"

IDirect3DTexture9* ObCastle::mTex = 0;
IDirect3DTexture9* ObCastle::mTexSM = 0;

ObCastle::ObCastle(){
	setId(Small);
	mInfo	= 0;
	mTexCR	= 0;

}

ObCastle::ObCastle(CASTLE nid){
	setId(nid);
	mInfo	= 0;
	mTexCR	= 0;
}

ObCastle::~ObCastle(){

}

bool ObCastle::setId(int nid){
	if(checkType(nid)){
		if(nid == Big){
			mID = Big;
			mWidth	= 112;
			mHeight = 112;
			mTexCR = mTex;
		}else{
			mID = Small;
			mWidth	= 80;
			mHeight = 80;
			mTexCR = mTexSM;
		}
		mCenter = D3DXVECTOR3(mWidth/2,mHeight/2,0.0f);
		return true;
	}else{
		return false;
	}
}

bool ObCastle::checkType(int obID){
	if(obID<Big || obID>Small){
		return false;
	}else{
		return true;
	}
}

void ObCastle::update(float dt){

}

void ObCastle::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0 || mTexSM == 0) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x, mPos.y- gCamera->getPos().y, 0);
	mSprite->SetTransform(&(S*R*T));
	mSprite->Draw(mTexCR,0,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false);
}

bool ObCastle::loadTexture(){
	if(mTex == 0){
		D3DXCreateTextureFromFileEx(gDevice,"Sprites/CastleBig.png",
			112,
			112,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex);
	}else{
		return false;
	}

	if(mTexSM == 0){
		D3DXCreateTextureFromFileEx(gDevice,"Sprites/Castle.png",
			80,
			80,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTexSM);
	}else{
		return false;
	}
	return true;
}

bool ObCastle::releaseTexture(){
	if(mTex != 0){
		ReleaseCOM(mTex);
	}else{
		return false;
	}

	if(mTexSM != 0){
		ReleaseCOM(mTexSM);
	}else{
		return false;
	}
	return true;
}
